package cn.semcorp.zhuhaizj.quartzJob;

import cn.semcorp.zhuhaizj.common.Common;
import cn.semcorp.zhuhaizj.exception.ServiceException;
import cn.semcorp.zhuhaizj.mapper.TrainingMapper;
import cn.semcorp.zhuhaizj.pojo.vo.SelectEducationPlan;
import cn.semcorp.zhuhaizj.pojo.vo.SelectSkillPlan;
import cn.semcorp.zhuhaizj.restful.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import static cn.semcorp.zhuhaizj.common.Common.*;
@Slf4j
public class TobePlanJob implements Job {
    @Autowired
    private TrainingMapper trainingMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        LocalDate nowDate = Common.getNowDate();
        String month = String.valueOf(nowDate.getMonthValue());
        String lastMonth = "";
        String lastYear = "";
        if (month.equals("1")){
            lastMonth = "12";
            lastYear = String.valueOf(nowDate.getYear() - 1);
        }else{
            lastMonth = String.valueOf(nowDate.getMonthValue()-1);
            lastYear = String.valueOf(nowDate.getYear());
        }
        String yearMonthLast = Common.formatterDateYYYY_MM(lastYear, lastMonth);
        // 定时任务待写
        selectNowMonthTobeCompleted(yearMonthLast,lastMonth);
        selectNowMonthTobeEducation(yearMonthLast,lastMonth);
    }


    /**
     * 监控当月培训计划是否都完成
     */
    public void selectNowMonthTobeCompleted(String yearMonthLast, String lastMonth){
        log.info("定时任务:技能认定年度计划延期状态更新中....");
        /**
         * 查是否延期
         */
        List<SelectSkillPlan> selectSkillPlan = trainingMapper.selectSkillPlanListInsert(yearMonthLast);
        // 上个月未完成计划 调整为延期
        if (selectSkillPlan.size() == 0){
            log.info("定时任务:技能认定年度计划无延期状态可更新,执行完成!");
            return;
        }
        for (SelectSkillPlan plan : selectSkillPlan) {
            if (plan.getState().equals("待完成")){
                trainingMapper.updateSkillPlanStateById(plan.getId(),"已延期");
                trainingMapper.updateSkillPlanCompleted("m"+lastMonth,icon3,plan.getProject());
            }
        }
        log.info("定时任务:技能认定年度计划延期状态更新完成!");
    }

    /**
     * 监控当月教育培训是否都完成
     */
    public void selectNowMonthTobeEducation(String yearMonthLast,String lastMonth){
        log.info("定时任务:教育培训年度计划延期状态更新中....");
        /**
         * 查是否延期
         */
        List<SelectEducationPlan> selectEducationPlanList = trainingMapper.selectEducationPlanListInsert(yearMonthLast);
        if (selectEducationPlanList.size() == 0){
            log.info("定时任务:教育培训年度计划无延期状态可更新,执行完成!");
            return;
        }
        for (SelectEducationPlan plan : selectEducationPlanList) {
            if (plan.getState().equals("待完成")){
                trainingMapper.updateEducationPlanStateById(plan.getId(),"已延期");
                trainingMapper.updateEducationPlanCompleted("m" + lastMonth, icon3, plan.getProject());
            }
        }
        log.info("定时任务:教育培训年度计划延期状态更新完成!");
    }


    public String getYearMonth(){
        LocalDate nowDate = Common.getNowDate();
        String year = String.valueOf(nowDate.getYear());
        String month = String.valueOf(nowDate.getMonthValue());
        return Common.formatterDate(year, month);
    }

}
